<?php

namespace App\Http\Extend;

use App\Models\Category;
use \Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;

trait TraitCategory
{

    public function Category()
    {

        if (request()->isMethod('post')) {
            return (Category::where("CategoryName", get_class($this))->select("*", 'pId as parent_id')->treeGrid());
        } else {
            return view('common.category.category',   F('Category', 'category', 'common'));
        }
    }
    public function CategoryAll()
    {
        return  Category::where("CategoryName", get_class($this))->select(DB::raw("case when sign is null then name else concat(name,'_',sign) end as name"), "id", "pId", "CategoryName")->get();
    }
    public function addCategory()
    {
        if (request()->isMethod('post')) {
            $data = request()->post('data');

            try {
                begin();

                $data['CategoryName'] = get_class($this);

                Category::create(array_filter($data));

                // $gdata->save();
                commit();
            } catch (\Exception $e) {
                rollback();
                return error($e->getMessage());
            }

            return success("保存成功！");
        } else {
            // $param = F();
            $param['selectdata']['data'] = Category::where("CategoryName", get_class($this))->select("*", 'pId as parent_id')->get();
            $param['selectdata']['name'] = "data['pId']";
            return view('common.category.addCategory',  $param);
        }
    }
    public function editCategory()
    {
        $id = request()->get('id');
        if ($id == 1)
            throwError("主类不能编辑！");
        $gdata = Category::where(['id' => $id])->first();
        $gdata->id = 321;
        if (request()->isMethod('post')) {
            $data = request()->all();

            begin();
            try {
                $gdata->update([
                    'pId' => $data['pId'],
                    'name' => $data['name'],
                    'sign' => $data['sign'],
                    'CategoryName' => get_class($this),
                ]);
                // Category::where(['id' => $id])->save();
                commit();
            } catch (\Exception $e) {
                rollback();
                return error($e->getMessage());
            }

            return success("保存成功！");
        } else {
            // $param = F();

            $param['selectdata']['data'] = Category::where("CategoryName", get_class($this))->select("*", 'pId as parent_id')->treeGrid(true);

            $param['selectdata']['name'] = "data['pId']";
            $param['selectdata']['default'] = $gdata->pId;
            $param['gdata'] = $gdata;

            return view('common.category.addCategory',  $param);
        }
    }
    public function deleteCategory()
    {
        begin();
        $ids = request()->post('ids');;
        // Category::whereIn("id", $ids)->delete();
        foreach (Category::whereIn("id", $ids)->get() as  $k => $v) {
            $v->delete();
        }
        commit();
        return success("删除成功！");
    }
}
